<?php

namespace App\Admin\Controllers\Sys;

use App\Models\Sys\Config;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ConfigController extends Controller
{
    use ModelForm;

    public $header = '系统配置';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());

            /*$box = new Widgets\Box('签到规则设置',$this->grid()->render());
            $content->row($box->solid()->style('primary'));*/

        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Config::class, function (Grid $grid) {
            $grid->sys_config_id('ID')->sortable();
            $grid->key('key');
            $grid->description('名称');
            $grid->value('值');
            $grid->status('状态');
            $grid->type('是否上线')->switch([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 2, 'text' => '否', 'color' => 'danger'],
            ]);
            $grid->p_type('平台类型')->using([
                '0' => '两端都可用',
                '1' => 'IOS',
                '2' => 'Android',
                '3' => 'web'
            ]);
            /*$grid->p_type('平台类型')->radio([
                '0' => '两端都可用',
                '1' => 'IOS',
                '2' => 'Android',
                '3' => 'web'
            ]);*/

            $grid->actions(function(Grid\Displayers\Actions $actions){
                $actions->disableDelete();
                $actions->disableView();
            });

            $grid->disableCreateButton();
            $grid->disableFilter();
            $grid->disableRowSelector();
            $grid->disablePagination();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Config::class, function (Form $form) {

            $form->display('sys_config_id', 'ID');
            $form->display('key', 'key');
            $form->display('description', '名称');
            $form->text('value', '值');
            $form->display('status', '状态');
            $form->switch('type','是否上线')->states([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 2, 'text' => '否', 'color' => 'danger'],
            ]);
            $form->radio('p_type', '平台类型')
                ->options([
                    '0' => '两端都可用',
                    '1' => 'IOS',
                    '2' => 'Android',
                    '3' => 'web']);

            $form->tools(function(Form\Tools $tools){
                $tools->disableView();
                $tools->disableDelete();
            });

            $form->footer(function ($footer){
                $footer->disableViewCheck();
                $footer->disableEditingCheck();
            });

            $form->saving(function (Form $form) {

                if ($form->type == 'on'){
                    $form->type = 1;
                } else {
                    $form->type = 2;
                }
            });

        });
    }

}
